﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DBService;
using YArchitech.Revit.MEP.Opening;

namespace HYRMEOpening
{
	public partial class DlgCheckOpening : System.Windows.Forms.Form
	{
		public Request Request
		{
			get
			{
				return this.m_request;
			}
			private set
			{
				this.m_request = value;
			}
		}

		public static DlgCheckOpening GetInstance(UIDocument doc, bool service, string fileName)
		{
			if (DlgCheckOpening.dco == null || DlgCheckOpening.dco.IsDisposed)
			{
				DlgCheckOpening.dco = new DlgCheckOpening(doc, service, fileName);
			}
			return DlgCheckOpening.dco;
		}

		public DlgCheckOpening(UIDocument doc, bool server, string fileName)
		{
			this.Request = new Request();
			this.IsServer = server;
			this.mUiDoc = doc;
			this.mFileName = fileName;
			List<DbOpeningBase> list = new List<DbOpeningBase>();
			if (server)
			{
				DbProject projectByCurrentDoc = DbProject.GetProjectByCurrentDoc(doc.Document);
				if (projectByCurrentDoc != null)
				{
					foreach (DbWallToBeOpened item in DbWallToBeOpened.GetByProject(projectByCurrentDoc))
					{
						list.Add(item);
					}
					foreach (DbFloorToBeOpened item2 in DbFloorToBeOpened.GetByProject(projectByCurrentDoc))
					{
						list.Add(item2);
					}
					using (List<DbBeamToBeOpened>.Enumerator enumerator3 = DbBeamToBeOpened.GetByProject(projectByCurrentDoc).GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							DbBeamToBeOpened item3 = enumerator3.Current;
							list.Add(item3);
						}
						goto IL_1FB;
					}
				}
				MessageBox.Show("服务器连接失败或此模型未加入任何项目，请配置服务器、创建项目后操作。");
			}
			else
			{
				FileInfo fileInfo = new FileInfo(this.mFileName);
				if (fileInfo.Exists)
				{
					foreach (DbWallToBeOpened item4 in DbWallToBeOpened.ReadFromXML(fileInfo.FullName))
					{
						list.Add(item4);
					}
					foreach (DbFloorToBeOpened item5 in DbFloorToBeOpened.ReadFromXML(fileInfo.FullName))
					{
						list.Add(item5);
					}
					using (List<DbBeamToBeOpened>.Enumerator enumerator3 = DbBeamToBeOpened.ReadFromXML(fileInfo.FullName).GetEnumerator())
					{
						while (enumerator3.MoveNext())
						{
							DbBeamToBeOpened item6 = enumerator3.Current;
							list.Add(item6);
						}
						goto IL_1FB;
					}
				}
				MessageBox.Show("此模型开洞数据文件不存在，请先运行开洞检测功能。");
				base.Close();
			}
			IL_1FB:
			this.OpiningList = new BindingList<DbOpeningBase>(list);
			DbOpeningBase.SortReportsByDisplayOrder(ref this.OpiningList);
			this.InitializeComponent();
			if (server)
			{
				this.rbByFile.Checked = false;
				this.rbByServer.Checked = true;
				this.tbFileName.Visible = false;
				return;
			}
			this.rbByFile.Checked = true;
			this.rbByServer.Checked = false;
			this.tbFileName.Visible = true;
			this.tbFileName.Text = fileName;
		}

		private void MakeRequest(RequestId request)
		{
			this.Request.Make(request);
		}

		private void TurnToView3D(UIDocument uiDoc)
		{
			if (!(uiDoc.ActiveView is View3D))
			{
				View3D view3D = null;
				Transaction transaction = new Transaction(uiDoc.Document, "DelView");
				try
				{
					transaction.Start();
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiDoc.Document);
					filteredElementCollector.OfClass(typeof(View3D));
					foreach (Element element in filteredElementCollector.ToElements())
					{
						if (element.Name == "盈建科开洞浏览")
						{
							view3D = (element as View3D);
							break;
						}
					}
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
				}
				if (view3D == null)
				{
					Transaction transaction2 = new Transaction(uiDoc.Document, "cv");
					try
					{
						transaction2.Start();
						FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(uiDoc.Document);
						filteredElementCollector2.WhereElementIsNotElementType();
						filteredElementCollector2.ToElements();
                        ViewFamilyType viewFamilyType = new FilteredElementCollector(uiDoc.Document).OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>().FirstOrDefault((ViewFamilyType x) => 102 == (int)x.ViewFamily);
						view3D = View3D.CreateIsometric(uiDoc.Document, viewFamilyType.Id);
						view3D.Name = "盈建科开洞浏览";
						view3D.DetailLevel = (ViewDetailLevel)3;
                        view3D.DisplayStyle = (DisplayStyle)6;
						new List<ElementId>();
						transaction2.Commit();
					}
					catch
					{
						transaction2.RollBack();
						base.Close();
					}
				}
				uiDoc.ActiveView = view3D;
			}
		}

		private void DlgCheckOpening_Load(object sender, EventArgs e)
		{
			this.TurnToView3D(this.mUiDoc);
			this.dgvOpeningList.DataSource = this.OpiningList;
			this.MakeRequest(RequestId.AddTag);
			DataGridViewComboBoxColumn dataGridViewComboBoxColumn = new DataGridViewComboBoxColumn();
			dataGridViewComboBoxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
			dataGridViewComboBoxColumn.DisplayIndex = 8;
			dataGridViewComboBoxColumn.DataSource = ConfigDataOperate.GetDrivePipeTypeName();
			dataGridViewComboBoxColumn.Name = "DrivePipeTypeName";
			dataGridViewComboBoxColumn.HeaderText = "套管类型";
			this.dgvOpeningList.Columns.Add(dataGridViewComboBoxColumn);
			dataGridViewComboBoxColumn.Visible = false;
			foreach (object obj in ((IEnumerable)this.dgvOpeningList.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				int num = dataGridViewRow.Index + 1;
				dataGridViewRow.Cells["colNumber"].Value = num.ToString();
				dataGridViewRow.Cells["DrivePipeTypeName"].Value = this.OpiningList[dataGridViewRow.Index].DrivePipeTypeName;
			}
			foreach (object obj2 in this.dgvOpeningList.Columns)
			{
				DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj2;
				dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
				dataGridViewColumn.DefaultCellStyle.BackColor = System.Drawing.Color.DarkGray;
				if (dataGridViewColumn.Name.Equals("IsCurrentProject") || dataGridViewColumn.Name.Equals("BoxParameterString"))
				{
					dataGridViewColumn.Visible = false;
				}
				else if (dataGridViewColumn.Name.Equals("IsOpened"))
				{
					dataGridViewColumn.HeaderText = "已开洞";
					dataGridViewColumn.ReadOnly = true;
				}
				else if (dataGridViewColumn.Name.Equals("RemarkString"))
				{
					dataGridViewColumn.ReadOnly = true;
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.ClearTag);
			base.Close();
		}

		private void DlgCheckOpening_FormClosed(object sender, FormClosedEventArgs e)
		{
			base.Dispose();
		}

		private void DlgCheckOpening_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.MakeRequest(RequestId.ClearTag);
		}

		private void dgvOpeningList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
		}

		private void dgvOpeningList_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void OnCheckElems()
		{
			if (this.dgvOpeningList.SelectedRows.Count > 0)
			{
				int index = this.dgvOpeningList.SelectedRows[0].Index;
				Element element = DbOpeningBase.FindTag(((BindingList<DbOpeningBase>)this.dgvOpeningList.DataSource)[index], this.mUiDoc.Document);
				if (element != null)
				{
					List<Element> list = new List<Element>();
					list.Add(element);
					RevitVersionFuncs.SetSelection(this.mUiDoc.Selection, list);
					UIView uiview = null;
					foreach (UIView uiview2 in this.mUiDoc.GetOpenUIViews())
					{
						if (uiview2.ViewId.Equals(this.mUiDoc.ActiveView.Id))
						{
							uiview = uiview2;
							break;
						}
					}
					XYZ xyz = element.get_BoundingBox(this.mUiDoc.ActiveView).Min;
					XYZ xyz2 = element.get_BoundingBox(this.mUiDoc.ActiveView).Max;
					XYZ xyz3 = xyz2 - xyz;
					xyz3 *= 10.0;
					xyz += xyz3;
					xyz2 -= xyz3;
					uiview.ZoomAndCenterRectangle(xyz, xyz2);
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
		}

		private void btnCheck_Click(object sender, EventArgs e)
		{
			this.OnCheckElems();
		}

		private void dgvOpeningList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvOpeningList.CurrentCell.OwningColumn.Name == "DrivePipeTypeName")
			{
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged -= this.ComboBox_SelectedIndexChanged;
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged += this.ComboBox_SelectedIndexChanged;
			}
		}

		private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.ComboBox comboBox = sender as System.Windows.Forms.ComboBox;
			if (comboBox == null || comboBox.SelectedItem == null)
			{
				return;
			}
			int rowIndex = this.dgvOpeningList.CurrentCell.RowIndex;
			string name = this.dgvOpeningList.CurrentCell.OwningColumn.Name;
			string drivePipeTypeName = comboBox.SelectedItem.ToString();
			if (name == "DrivePipeTypeName")
			{
				this.OpiningList[rowIndex].DrivePipeTypeName = drivePipeTypeName;
			}
		}

		private void DlgCheckOpening_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					base.Close();
				}
			}
			catch
			{
			}
		}

		private void DlgCheckOpening_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.OnCheckElems();
		}

		private void dgvOpeningList_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.OnCheckElems();
		}

		public UIDocument mUiDoc;

		public List<FamilyInstance> FamilyInstanceList = new List<FamilyInstance>();

		public bool IsServer;

		private string mFileName;

		public BindingList<DbOpeningBase> OpiningList = new BindingList<DbOpeningBase>();

		private Request m_request;

		private static DlgCheckOpening dco;
	}
}
